package com.anolesoft.amljmes.fortyone.controller;

import java.util.List;

import com.anolesoft.amljmes.fortyone.dto.*;
import com.anolesoft.amljmes.fortyone.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.anolesoft.amljmes.fortyone.entity.BasRclMachines;
import com.anolesoft.amljmes.fortyone.entity.MesGjgyxxFksz;
import com.anolesoft.amljmes.fortyone.service.MesStoveBaseService;
import com.anolesoft.amljmes.fortyone.service.MesStoveFabinfoService;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 排炉派工检验相关API
 * 
 * @author 陈向前
 *
 */
@RestController
@RequestMapping("/stove")
public class MesStoveController {

	@Autowired
	private MesStoveBaseService mesStoveBaseService;
	@Autowired
	private MesStoveFabinfoService mesStoveFabinfoService;
	
	/**
	 * 保存炉次检验信息
	 * @param map
	 * @return
	 */
	@PostMapping(value = "/saveStoveInpect")
	public AjaxResult saveStoveInpect(@RequestBody StoveInpectJsonBean stoveInpectJsonBean) {
		
		int aa = mesStoveFabinfoService.updateMesStoveFabinfoSl(stoveInpectJsonBean);
		
		if(stoveInpectJsonBean.getData().size() > 0)
		mesStoveBaseService.updateMesStoveBaseWj(stoveInpectJsonBean.getData().get(0).getStoyeFabinfoObj());
		if(aa == 2 ) {
			return new AjaxResult(HttpStatus.NOT_MODIFIED, "检验条码错误",aa);
		}else {
			return  AjaxResult.success();
		}
		
	}
	/**
	 * 获取工艺方法简称下拉选
	 * 
	 * @param keyWord 
	 * @return
	 */
	@GetMapping(value = "/getCappSimpleNameOptions")
	public AjaxResult getCappSimpleNameOptions(@RequestParam(value = "keyWord", required = false) String keyWord) {
		List<MesGjgyxxFksz> mesGjgyxxFkszs = mesStoveBaseService.getCappSimpleNameOptions(keyWord);
		return AjaxResult.success(mesGjgyxxFkszs);
	}

	/**
	 * 获取设备下拉选
	 * 
	 * @param keyWord
	 * @return
	 */
	@GetMapping(value = "/getDevicenameOptions")
	public AjaxResult getDevicenameOptions(@RequestParam(value = "keyWord", required = false) String keyWord) {
		List<BasRclMachines> devicenameOptions = mesStoveBaseService.getDevicenameOptions(keyWord);
		return AjaxResult.success(devicenameOptions);
	}

	/**
	 * 保存炉次信息
	 * 
	 * @param stoveSaveDTOJson
	 * @return
	 */
	@PostMapping(value = "/saveStove")
	public AjaxResult saveStove(@RequestBody StoveSaveDTO stoveSaveDTO) {
		mesStoveBaseService.saveStove(stoveSaveDTO);
		return AjaxResult.success();
	}

	/**
	 * 删除炉次
	 * 
	 * @param objuids
	 * @return
	 */
	@PostMapping(value = "/deleteStove")
	public AjaxResult deleteStove(@RequestBody List<String> objuids) {
		mesStoveBaseService.deleteMesStoveBase(objuids);
		return AjaxResult.success();
	}

	/**
	 * 炉次派工主页查询接口
	 * 
	 * @param stoveQueryParamDto
	 * @return
	 */
	@PostMapping(value = "/getStoveList")
	public AjaxResult getStoveList(@RequestBody StoveQueryParamDto stoveQueryParamDto) {
		Page<StoveVo> pageInfo = mesStoveBaseService.getStoveList(stoveQueryParamDto);
		return AjaxResult.success(pageInfo);
	}

	/**
	 * 炉次信息修改
	 *
	 * @param fortyoneApplyDto
	 * @return
	 */
	@PostMapping(value = "/getFortyoneApplyList")
	public AjaxResult getFortyoneApplyList(@RequestBody FortyoneApplyDto fortyoneApplyDto) {
		Page<FortyoneApplyVo> pageInfo = mesStoveBaseService.getFortyoneApplyList(fortyoneApplyDto);
		return AjaxResult.success(pageInfo);
	}

	@PostMapping(value = "/getFortyoneExamineList")
	public AjaxResult getFortyoneExamineList(@RequestBody FortyoneApplyDto fortyoneApplyDto) {
		Page<MesQualityUpdatelogVo> pageInfo = mesStoveBaseService.getFortyoneExamineList(fortyoneApplyDto);
		return AjaxResult.success(pageInfo);
	}

	@PostMapping(value = "/getFortyoneAdoptList")
	public AjaxResult getFortyoneAdoptList(@RequestBody FortyoneApplyDto fortyoneApplyDto) {
		Page<MesQualityUpdatelogVo> pageInfo = mesStoveBaseService.getFortyoneAdoptList(fortyoneApplyDto);
		return AjaxResult.success(pageInfo);
	}

	@PostMapping(value = "/getFortyoneHistoryList")
	public AjaxResult getFortyoneHistoryList(@RequestBody FortyoneApplyDto fortyoneApplyDto) {
		Page<MesQualityUpdatelogVo> pageInfo = mesStoveBaseService.getFortyoneHistoryList(fortyoneApplyDto);
		return AjaxResult.success(pageInfo);
	}

	@PostMapping(value = "/getFortyoneList")
	public AjaxResult getFortyoneList(@RequestBody FortyoneApplyDto fortyoneApplyDto) {
		List<FortyoneVo> list = mesStoveBaseService.getFortyoneList(fortyoneApplyDto.getCondition().getObjuid());
		return AjaxResult.success(list);
	}

	@PostMapping(value = "/getFortyoneListNew")
	public AjaxResult getFortyoneListNew(@RequestBody FortyoneApplyDto fortyoneApplyDto) {
		List<FortyoneVo> list = mesStoveBaseService.getFortyoneListNew(fortyoneApplyDto.getCondition().getObjuid());
		return AjaxResult.success(list);
	}

	@PostMapping(value = "/go")
	public AjaxResult go(@RequestBody FortyoneApplyDto fortyoneApplyDto) {
		mesStoveBaseService.go(fortyoneApplyDto.getCondition().getObjuid());
		return AjaxResult.success();
	}

	@PostMapping(value = "/adopt")
	public AjaxResult adopt(@RequestBody FortyoneApplyDto fortyoneApplyDto) {
		mesStoveBaseService.adopt(fortyoneApplyDto.getCondition().getObjuid());
		return AjaxResult.success();
	}

	@PostMapping(value = "/back")
	public AjaxResult back(@RequestBody FortyoneApplyDto fortyoneApplyDto) {
		mesStoveBaseService.back(fortyoneApplyDto.getCondition().getObjuid());
		return AjaxResult.success();
	}

	@PostMapping(value = "/getYz")
	public AjaxResult getYz(@RequestBody ModifyDto modifyDto) {
		String yz = mesStoveBaseService.getYz(modifyDto.getCondition().getObjuid(),modifyDto.getCondition().getZljl());
		ModifyVo modifyVo = new ModifyVo();
		modifyVo.setYz(yz);
		return AjaxResult.success(modifyVo);
	}

	@PostMapping(value = "/saveModifyApply")
	public AjaxResult saveModifyApply(@RequestBody ModifyDto modifyDto) {
		mesStoveBaseService.saveModifyApply(modifyDto);
		return AjaxResult.success();
	}

	/**
	 * 炉次派工，扫码派工也制造记录条码校验
	 * 
	 * @param scanIn
	 * @param mesStoveBaseUid
	 * @param gyffjc
	 * @return
	 */
	@GetMapping(value = "/dispatchScanIn")
	public AjaxResult dispatchScanIn(String scanIn,
			@RequestParam(value = "mesStoveBaseUid", required = true) String mesStoveBaseUid,
			@RequestParam(value = "gxmgjzf", required = true) String gxmgjzf) {
		MesOrderTraceInfoDto dto = mesStoveBaseService.checkStoveDispatchScanIn(scanIn, mesStoveBaseUid, gxmgjzf);
		mesStoveBaseService.saveMesStoveFabinfo(dto);
		mesStoveBaseService.updateDispatchCountAndState(mesStoveBaseUid);
		return AjaxResult.success();
	}

	/**
	 * 炉次派工，扫码派工界面查询结果列表
	 * 
	 * @param paramDto
	 * @return
	 */
	@PostMapping(value = "/getDispatchList")
	public AjaxResult getDispatchList(@RequestBody StoveDispatchQueryParamDto paramDto) {
		Page<MesStoveFabinfoVo> page = mesStoveFabinfoService.getMesStoveFabinfoVOs(paramDto);
		return AjaxResult.success(page);
	}

	/**
	 * 炉次派工，扫码派工界面删除已派工的信息
	 * 
	 * @param objuids
	 * @return
	 */
	@PostMapping(value = "/deleteDispatch")
	public AjaxResult deleteDispatch(@RequestBody StoveDispatchDeleteParamDto stoveDispatchDeleteParamDto) {
		mesStoveFabinfoService.deleteDispatch(stoveDispatchDeleteParamDto.getObjuids());
		mesStoveBaseService.updateDispatchCountAndState(stoveDispatchDeleteParamDto.getCurStoveUid());
		return AjaxResult.success();
	}

	/**
	 * 炉次开工
	 * 
	 * @param objuids
	 * @return
	 */
	@PostMapping(value = "/startWorking")
	public AjaxResult startWorking(@RequestBody List<String> objuids) {
		mesStoveBaseService.updateMesStoveBaseKg(objuids);
		mesStoveBaseService.updateProcessKg(objuids);
		/*
		 * 这里是否需要修改颜色表 mes_process_screen
		 */
		return AjaxResult.success();
	}

	/**
	 * 炉次完工
	 * 
	 * @param objuids
	 * @return
	 */
	@PostMapping(value = "/finishWorking")
	public AjaxResult finishWorking(@RequestBody List<String> objuids) {
		mesStoveBaseService.updateMesStoveBaseWg(objuids);
		return AjaxResult.success();
	}

	/**
	 * 获取质量表单信息
	 * 
	 * @param objuid
	 * @return
	 */
	@GetMapping(value = "/getQualityFormInfo")
	public AjaxResult getQualityFormInfo(@RequestParam String objuid) {
		QualityFormVo vo = mesStoveBaseService.getQualityFormInfo(objuid);
		return AjaxResult.success(vo);
	}

	/**
	 * 保存炉次质量信息
	 * 
	 * @param saveQualityDto
	 * @return
	 */
	@PostMapping(value = "/saveQualityFormInfo")
	public AjaxResult saveQualityFormInfo(@RequestBody SaveQualityDto saveQualityDto) {
		mesStoveBaseService.saveQualityFormInfo(saveQualityDto);
		return AjaxResult.success();
	}

	/**
	 * 质量表单检验员扫码
	 * 
	 * @param saveQualityDto
	 * @return
	 */
	@PostMapping(value = "/qualityFormcheckInput")
	public AjaxResult checkInput(@RequestBody SaveQualityDto saveQualityDto) {
		mesStoveBaseService.qualityFormcheckInput(saveQualityDto);
		return AjaxResult.success();
	}
}
